package cn.lili.modules.member.entity.dto;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.modules.member.entity.dos.UserAddress;
import cn.lili.modules.member.entity.enums.AddressTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Objects;

/**
 * 地址检索参数
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/4/21 14:58
 */
@Data
public class AddressSearchParams {

    @ApiModelProperty(value = "id")
    private String id;

    @ApiModelProperty(value = "地址类型")
    private AddressTypeEnum type = AddressTypeEnum.RECEIVE;

    @ApiModelProperty(value = "场景")
    private SceneEnums scene = SceneEnums.MEMBER;

    @ApiModelProperty(value = "扩展id")
    private String extendId;

    public QueryWrapper<UserAddress> queryWrapper() {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        switch (Objects.requireNonNull(UserContext.getCurrentUser()).getScene()) {
            case SUPPLIER, STORE, MEMBER -> {
                this.scene = UserContext.getCurrentUser().getScene();
                this.extendId = UserContext.getCurrentUser().getExtendId();
            }
            case MANAGER -> this.scene = null;
            default -> {
            }
        }

        if (type != null) {
            queryWrapper.eq("type", this.type.name());
        }
        if (scene != null) {
            queryWrapper.eq("scene", this.scene.name());
        }
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(id), "id", this.id);
        queryWrapper.eq(extendId != null, "extend_id", this.extendId);
        return queryWrapper;
    }

    public QueryWrapper<UserAddress> queryWrapperNoAuth() {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(CharSequenceUtil.isNotEmpty(id), "id", this.id);
        if (type != null) {
            queryWrapper.eq("type", this.type.name());
        }
        if (scene != null) {
            queryWrapper.eq("scene", this.scene.name());
        }
        queryWrapper.eq(extendId != null, "extend_id", this.extendId);
        return queryWrapper;
    }

}
